<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Im_category extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
	}

	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'),array(
	  		'case_type'			=>	$this->wus->get_lookup_values_by_type('CASE_TYPE'),
	  		'category'			=>	$this->wus->get_lookup_values_by_type('CATEGORY'),
	  		'sub_category'		=>	$this->wus->get_lookup_values_by_type('SUB_CATEGORY'),
	  		'department'		=>	$this->wus->get_lookup_values_by_type('DEPARTMENT'),
	  		'activity_status'	=>	$this->wus->get_lookup_values_by_type('ACTIVITY_STATUS'),
	  		'case_status'		=>	$this->wus->get_lookup_values_by_type('CASE_STATUS'),
	  ));
	  $this->load->view('layouts/footer');
	}
	
	public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array(
				'department'		=>	$this->wus->get_lookup_values_by_type('DEPARTMENT'),
				'assigned_to'		=>	$this->im_category_model->get_users(),
			));
	}

	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'),array(
				'department'		=>	$this->wus->get_lookup_values_by_type('DEPARTMENT'),
				'assigned_to'		=>	$this->im_category_model->get_users(),
				'category'			=>	$this->im_category_model->get_categories($id),
			));
	}

	public function view_form($id){
		$this->load->view($this->session->userdata('forview'),array(
				'department'		=>	$this->wus->get_lookup_values_by_type('DEPARTMENT'),
				'assigned_to'		=>	$this->im_category_model->get_users(),
				'category'			=>	$this->im_category_model->get_categories($id),
			));
	}

	public function search_num(){
		echo json_encode($this->im_category_model->search_num($this->input->post('caller_id')));		
	}

	public function get_sub_category(){
		echo json_encode($this->im_category_model->get_sub_category($this->input->post('category_id')));
	}

	public function get_history_by_case_id(){
		echo json_encode($this->im_category_model->get_history_by_case_id($this->input->post('id')));
	}

	public function get_sub_category_by_id(){
		echo json_encode($this->im_category_model->get_sub_category_by_id($this->input->post('id')));
	}

	public function delete_sub_category(){
		$this->im_category_model->delete_sub_category($this->input->post('id'));
		echo json_encode(array('err' => 0, "msg" => NULL));
	}

	public function get_user_by_department(){
		echo json_encode($this->im_category_model->get_user_by_department($this->input->post('id')));
	}

	public function get_user_by_department_with_id(){
		echo json_encode($this->im_category_model->get_user_by_department($this->input->post('cat'),$this->input->post('ass')));
	}

	public function search(){
		$data = array(
			'category_code'			=>	$this->input->post('category_code'),
			'category_name'			=>	$this->input->post('category_name')
		);
		echo json_encode($this->im_category_model->search($data));
	}

	public function insert(){
		$data = array(
			'p_case_code'			=>	$this->input->post('category_code'),
			'p_case_name'			=>	$this->input->post('category_name'),
			'p_description'			=>	$this->input->post('description'),
			'p_default_department'  =>	$this->input->post('default_department'),
			'p_default_assign_to'	=>	$this->input->post('default_assign_to'),
			'p_user_id'				=>	$this->session->userdata('user_id')
		);
		$this->im_category_model->insert_category($data);
	}

	public function update(){
		$data = array(
			'p_case_code'			=>	$this->input->post('category_code'),
			'p_case_name'			=>	$this->input->post('category_name'),
			'p_description'			=>	$this->input->post('description'),
			'p_default_department'  =>	$this->input->post('default_department'),
			'p_default_assign_to'	=>	$this->input->post('default_assign_to'),
			'p_user_id'				=>	$this->session->userdata('user_id'),
			'p_category_id'			=>	$this->input->post('category_id')
		);
		$this->im_category_model->update_category($data);
	}

	public function insert_sub(){
		$data = array(
			'p_case_code'			=>	$this->input->post('sub_category_code'),
			'p_case_name'			=>	$this->input->post('sub_category_name'),
			'p_description'			=>	$this->input->post('description'),
			'p_default_department'  =>	$this->input->post('default_department'),
			'p_default_assign_to'	=>	$this->input->post('default_assign_to'),
			'p_user_id'				=>	$this->session->userdata('user_id'),
			'p_category_id'			=>	$this->input->post('category_id')
		);
		$this->im_category_model->insert_sub_category($data);
	}

	public function update_sub(){
		$data = array(
			'p_case_code'			=>	$this->input->post('sub_category_code'),
			'p_case_name'			=>	$this->input->post('sub_category_name'),
			'p_description'			=>	$this->input->post('description'),
			'p_default_department'  =>	$this->input->post('default_department'),
			'p_default_assign_to'	=>	$this->input->post('default_assign_to'),
			'p_user_id'				=>	$this->session->userdata('user_id'),
			'p_sub_category_id'		=>	$this->input->post('sub_category_id')
		);

		$this->im_category_model->update_sub_category($data);
	}

	public function insert_history(){
		$data = array(
			'p_case_id'				=>	$this->input->post('case_id'),
			'p_action'				=>	$this->input->post('action'),
			'p_comments'			=>	$this->input->post('comments'),
			'p_root_cause'			=>	$this->input->post('root_cause'),
			'p_related_doc_type'	=>	$this->input->post('related_doc_type'),
			'p_related_doc_no'		=> 	$this->input->post('related_doc_no'),
			'p_action_date'			=>	$this->input->post('action_date'),
			'p_user_id'				=> 	$this->session->userdata('user_id'),
			'p_assigned_to'			=>	$this->input->post('assigned_to')
		);
		$this->im_category_model->insert_history($data);
	}
}